package com.mingsoft.cms.action.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mingsoft.base.filter.DateValueFilter;
import com.mingsoft.base.filter.DoubleValueFilter;
import com.mingsoft.basic.action.BaseAction;
import com.mingsoft.basic.biz.IColumnBiz;
import com.mingsoft.basic.entity.CategoryEntity;
import com.mingsoft.basic.entity.ColumnEntity;
import com.mingsoft.cms.biz.IBasePropertyBiz;
import com.mingsoft.cms.biz.ICompanyBiz;
import com.mingsoft.cms.biz.IGoodsChooseBiz;
import com.mingsoft.cms.biz.IInsuranceBiz;
import com.mingsoft.cms.biz.IPropertyValueBiz;
import com.mingsoft.cms.constant.ModelCode;
import com.mingsoft.cms.entity.BasePropertyEntity;
import com.mingsoft.cms.entity.GoodsChooseEntity;
import com.mingsoft.cms.entity.InsuranceEntity;
import com.mingsoft.cms.entity.InsurancePriceEntity;
import com.mingsoft.cms.entity.InsurancePropertyEntity;
import com.mingsoft.cms.entity.PropertyValueEntity;
import com.mingsoft.cms.entity.InsuranceEntity;
import com.mingsoft.util.JsonUtil;
import com.mingsoft.util.StringUtil;

import net.mingsoft.base.util.JSONObject;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.util.BasicUtil;

@Controller("goodsChoose")
@RequestMapping("/${managerPath}/goods/goodsChoose")
public class GoodsChooseAction extends BaseAction{
    @Autowired
    private IGoodsChooseBiz goodsChooseBiz;
    @Autowired
	private IColumnBiz columnBiz;
   
    /**
     * 返回主界面index
     */
    @RequestMapping("/index")
    public String index(HttpServletResponse response,HttpServletRequest request){
    	// 获取站点id
    	List<ColumnEntity> list = getChildList(149);
    	request.setAttribute("listColumn", JSONArray.toJSONString(list));
    	return view ("/goods/goodsChoose_index");
    }
    /**
     * 返回主界面index
     */
    @RequestMapping("/main")
    public String main(HttpServletResponse response,HttpServletRequest request,ModelMap model){
    	
    	
    	return view ("/goods/goodsChoose_main");
    }
  
    /**
     * 保险产品列表
     * @param insurance
     * @param response
     * @param request
     * @param model
     */
    @RequestMapping("/list")
    @ResponseBody
    public void list(@ModelAttribute GoodsChooseEntity goodsChoose,HttpServletResponse response, HttpServletRequest request,ModelMap model) {
        
        String isParent = request.getParameter("isParent");
       
        
        BasicUtil.startPage();
        List<InsuranceEntity> basePropertyList = goodsChooseBiz.query(goodsChoose);
//        List<InsuranceEntity> basePropertyList = insuranceBiz.query(1, 0, null, null, null, true, insurance);
        this.outJson(response, net.mingsoft.base.util.JSONArray.toJSONString(new EUListBean(basePropertyList,(int)BasicUtil.endPage(basePropertyList).getTotal()),new DoubleValueFilter(),new DateValueFilter()));
    }

    /**
     * 返回编辑界面baseProperty_form
     */
    @RequestMapping("/form")
    public String form(@ModelAttribute GoodsChooseEntity goodsChoose,HttpServletResponse response,HttpServletRequest request,ModelMap model){
    	
    	GoodsChooseEntity gc = new GoodsChooseEntity();
    	if(goodsChoose.getGcId() != null){
    		gc = (GoodsChooseEntity) goodsChooseBiz.getEntity(goodsChoose.getGcId());            
    		model.addAttribute("categroyId",gc.getCategroyId());
    	}
        
    	 model.addAttribute("goodsChoose",gc);
    	 
        List companyList = goodsChooseBiz.queryAll();
        model.addAttribute("companyList",companyList);
        
        List<ColumnEntity> columnList = getChildList(149);
        model.addAttribute("columnList", JSONArray.toJSONString(columnList));
       
        
        return view ("/goods/goodsChoose_from");
    }


    /**
     * 获取保险信息
     * @param insurance
     * @param response
     * @param request
     * @param model
     */
    @RequestMapping("/get")
    @ResponseBody
    public void get(@ModelAttribute InsuranceEntity insurance,HttpServletResponse response, HttpServletRequest request,ModelMap model){
        if( 0 == insurance.getInsId()  ) {
            this.outJson(response, null, false, getResString("err.error", this.getResString("insurance.companyId")));
            return;
        }
        InsuranceEntity _insurance = (InsuranceEntity)goodsChooseBiz.getEntity(insurance);
        this.outJson(response, _insurance);
    }


    /**
     * 保存保险信息
     * @param goodsChoose
     * @param response
     * @param request
     */
    @PostMapping("/save")
    @ResponseBody
    public void save(@ModelAttribute GoodsChooseEntity goodsChoose, HttpServletResponse response, HttpServletRequest request) {
       
        
        goodsChoose.setStatus(0);
        goodsChoose.setCreateDate(new Date());
        goodsChooseBiz.saveEntity(goodsChoose);//如果是普通模版 直接调用saveEntity(company)
        
      
        this.outJson(response, JSONObject.toJSONString(goodsChoose));
    }

    /**
     * 更新保险信息
     * @param goodsChoose
     * @param response
     * @param request
     */
    @RequestMapping("/delete")
    @RequiresPermissions("insurance:delete")
    @ResponseBody
    public void delete(@RequestBody List<GoodsChooseEntity> goodsChoose,HttpServletResponse response, HttpServletRequest request) {
        int[] ids = new int[goodsChoose.size()];
        for(int i = 0;i<goodsChoose.size();i++){
            int insId = goodsChoose.get(i).getGcId();
			
        	ids[i] = insId;
        }
        goodsChooseBiz.deleteBasic(ids);    
        this.outJson(response, true);
    }
    
    

   
    @PostMapping("/update")
    @ResponseBody     
    public void update(@ModelAttribute GoodsChooseEntity goodsChoose, HttpServletResponse response,
            HttpServletRequest request) {
       
        goodsChoose.setUpdateDate(new Date());
        goodsChooseBiz.updateEntity(goodsChoose);//如果是普通模版 直接调用updateEntity(baseProperty)
        
        this.outJson(response, JSONObject.toJSONString(goodsChoose));
    }
    
    
	public List<ColumnEntity> getChildList(int parentId) {
		List<ColumnEntity> list = new ArrayList<ColumnEntity>();

		List<ColumnEntity> tempList = columnBiz.queryChildListByColumnId(149, null);
		list.addAll(tempList);
		for (ColumnEntity ce : tempList) {

			List<ColumnEntity> twoCategoryList = columnBiz.queryChildListByColumnId(ce.getCategoryId(), null);
			list.addAll(twoCategoryList);
			for (ColumnEntity ce2 : twoCategoryList) {

				List<ColumnEntity> threeCategoryList = columnBiz.queryChildListByColumnId(ce2.getCategoryId(), null);
				list.addAll(threeCategoryList);

			}

		}

		return list;
	}

}